package com.pozu.new_retail.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.VisitRecord;
import com.pozu.new_retail.service.AppSettingService;
import com.pozu.new_retail.service.CategoryService;
import com.pozu.new_retail.service.GroupSettingService;
import com.pozu.new_retail.service.SlideshowService;
import com.pozu.new_retail.service.VisitRecordService;
import com.pozu.new_retail.utils.COSSTS;
import com.pozu.new_retail.utils.CacheManager;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.LogUtils;
import com.pozu.new_retail.utils.MsmUtil;
import com.pozu.new_retail.utils.Tools;

@Controller
public class CommonController {
	
	@Autowired
	private AppSettingService appSettingService;
	@Autowired
	private SlideshowService slideshowService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private GroupSettingService groupSettingService;
	@Autowired
	private VisitRecordService visitRecordService;
	
	@ResponseBody
	@RequestMapping(value = "/api/verifyCodes", method = RequestMethod.GET)
	public Object getCodes(HttpServletRequest request) throws Exception{
		
		String nationCode = request.getParameter("nationCode");//国家代码(目前只接受大陆11位的的手机号)
		String phone = request.getParameter("phone");//手机号

		JsonResult jr = new JsonResult();
		
		if(phone == null || !phone.matches("^[0-9]{11}$")){
			jr.respCode = CodeConstants.UNPHONENUM_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.UNPHONENUM_MSG);
			LogUtils.INFO.info("{}手机号格式不正确",phone);
			return jr;
		}
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		MsmUtil.send(nationCode,phone);
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/verifyCodes", method = RequestMethod.POST)
	public Object checkCodes(HttpServletRequest request) throws Exception{

		Map<String, Object> map = Tools.getRequestData(request);
		
		String nationCode = map.get("nationCode").toString();//国家代码(目前只接受大陆11位的的手机号)
		String phone = map.get("phone").toString();//手机号
		String code = map.get("code").toString();//验证码
	
		JsonResult jr = new JsonResult();
		
		Long saveTime = CacheManager.getData(nationCode+phone+","+code);
		
		if(null == saveTime || System.currentTimeMillis() - saveTime >= 5*60*1000){
			jr.respCode = CodeConstants.PHONECODEERROR_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.PHONECODEERROR_MSG);
			LogUtils.INFO.info("{}手机验证码错误,{}",nationCode+phone,code);
			return jr;
		}
		
		CacheManager.clear(nationCode+phone);
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/authTokens", method = RequestMethod.GET)
	public Object getAuthToken(HttpServletRequest request) throws Exception{
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = COSSTS.getSTS();
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/appSettings", method = RequestMethod.GET)
	public Object getAppSetting(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = appSettingService.querySettingByAppId(appId);
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/groupSettings", method = RequestMethod.GET)
	public Object getGroupSetting(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = groupSettingService.queryByAppId(appId);
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/slideshows", method = RequestMethod.GET)
	public Object getSlideshow(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = slideshowService.queryByAppId(appId);
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/categorys", method = RequestMethod.GET)
	public Object getCategorys(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = categoryService.queryByAppId(appId);
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/ads", method = RequestMethod.GET)
	public Object getAds(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = appSettingService.queryAds(appId);
		
		return jr;		
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/visitRecords", method = RequestMethod.POST)
	public Object addVisitRecord(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		int channelId = request.getHeader("channelId")==null?1:Integer.parseInt(request.getHeader("channelId"));//渠道
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		VisitRecord visitRecord = new VisitRecord();
		visitRecord.setAppId(appId);
		visitRecord.setChannelId(channelId);
		visitRecord.setUserId(userId);
		
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		visitRecordService.saveVisitRecord(visitRecord);
		
		return jr;		
	}

}
